







//import java.net.MalformedURLException;
//import java.nio.file.Path;


import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;



import db.DBManager;



/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class FilesWindow extends org.eclipse.swt.widgets.Composite {
	private Group PlugInGroup;
	private Button tag1Button;
	private Button searchButton;
	public Text otherText;
	private Button tagsButton;
	private Button applyButton;
	private List filesList;
	private Text tagsSearchText;
	private Group filesGroup;
	public  Button browseButton;
	private Button otherButton;
	private Button tag3Button;
	private Button tag2Button;
	private Listener listener;
	private DBManager dbm = new DBManager();
	private String[] searchList;
	private StartTag st = new StartTag("");

	/**
	 * Auto-generated main method to display this 
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	public static void main(String[] args) {
		showGUI();
	}

	/**
	 * Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	 */	
	protected void checkSubclass() {
	}

	/**
	 * Auto-generated method to display this 
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	public static void showGUI() 
	{
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		FilesWindow inst = new FilesWindow(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) 
		{
			inst.pack();
			shell.pack();
		} else 
		{
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) 
		{
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public FilesWindow(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() 
	{
		try 
		{
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			this.setSize(532, 506);

			filesGroup = new Group(this, SWT.NONE);
			filesGroup.setLayout(null);
			FormData filesGroupLData = new FormData();
			filesGroupLData.left =  new FormAttachment(0, 1000, 12);
			filesGroupLData.top =  new FormAttachment(0, 1000, 137);
			filesGroupLData.width = 502;
			filesGroupLData.height = 340;
			filesGroup.setLayoutData(filesGroupLData);
			filesGroup.setText("Files :");

			tagsSearchText = new Text(filesGroup, SWT.WRAP | SWT.H_SCROLL | SWT.BORDER);
			tagsSearchText.setBounds(56, 19, 388, 26);
			//		tagsSearchText.setEditable(false);
			tagsSearchText.setEnabled(false);

			listener = new Listener() {

				@Override
				public void handleEvent(Event arg0) {


				}
			};

			tagsSearchText.addListener(SWT.FOCUSED,listener);


			tagsButton = new Button(filesGroup, SWT.PUSH | SWT.CENTER);
			tagsButton.setText("Tags");
			tagsButton.setBounds(12, 19, 38, 26);
			listener = new Listener() {


				@Override
				public void handleEvent(Event arg0) {

					SelectDialog sd = new SelectDialog(getShell(), getStyle());
					String[] text;
					text = parseString(tagsSearchText.getText());
					searchList = sd.open(text);
					//System.out.println(searchList.length);
					int i=0;
					String str = "";
					//System.out.println(str + " = str");
					if (searchList!=null && searchList.length != 0 ){
						while (i < searchList.length - 1){
							str+=searchList[i];
							str+=" , ";
							i++;
						}
						str+=searchList[searchList.length-1];
						tagsSearchText.setText(str);
					}
					else{
						tagsSearchText.setText("");
					}


				}
			};

			tagsButton.addListener(SWT.Selection,listener);


			filesList = new List(filesGroup, SWT.BORDER);
			filesList.setBounds(12, 57, 484, 288);
			{
				searchButton = new Button(filesGroup, SWT.PUSH | SWT.CENTER);
				searchButton.setText("Search");
				searchButton.setBounds(450, 19, 46, 26);
				listener = new Listener()
				{

					@Override
					public void handleEvent(Event event) 
					{
						if(event.widget == searchButton)
						{
							filesList.removeAll();
							String[] files;
							int i = 0;
							while (searchList!=null && i<searchList.length ){
								files = dbm.getFilesByTag(searchList[i]);
								int j = 0;
								while (j<files.length)
								{
									if(isExist(filesList,files[j])==false){
										System.out.println("ADD "+ files[j]);
										filesList.add(files[j]);
									}
									j++;
								}
								i++;
							}
							System.out.println("OUT OF WHILE");
						}

					}

					private boolean isExist(List filesList, String str) {
						System.out.println("files list length is "+filesList.getItems().length);
						System.out.println("the files name is "+ str);
				//		System.out.println("IN IS EXIST");
						int i = 0;
						String[] tempList = filesList.getItems();
						while(i<tempList.length){
							if (str.equals(tempList[i])==true){
								System.out.println("OUT FALSE IS EXIST");
								return true;
							}
							i++;
						}
						System.out.println("OUT TRUE IS EXIST");
						return false;

					}

				};
				searchButton.addListener(SWT.Selection, listener);
			}

			PlugInGroup = new Group(this, SWT.NONE);
			FormLayout PlugInGroupLayout = new FormLayout();
			PlugInGroup.setLayout(PlugInGroupLayout);
			FormData PlugInGroupLData = new FormData();
			PlugInGroupLData.left =  new FormAttachment(0, 1000, 12);
			PlugInGroupLData.top =  new FormAttachment(0, 1000, 12);
			PlugInGroupLData.width = 502;
			PlugInGroupLData.height = 96;
			PlugInGroup.setLayoutData(PlugInGroupLData);
			PlugInGroup.setText("Choose a PlugIn to tag your files :");
			{
				applyButton = new Button(PlugInGroup, SWT.PUSH | SWT.CENTER);
				FormData applyButtonLData = new FormData();
				applyButtonLData.left =  new FormAttachment(0, 1000, 454);
				applyButtonLData.top =  new FormAttachment(0, 1000, 60);
				applyButtonLData.width = 42;
				applyButtonLData.height = 24;
				applyButton.setLayoutData(applyButtonLData);
				applyButton.setText("Apply");
				applyButton.setEnabled(false);
				listener = new Listener()
				{

					@Override
					public void handleEvent(Event event) 
					{
						if(event.widget == applyButton)
						{
							try {
								Path p = Paths.get("C:\\Users\\chen\\Desktop\\EntryGui.txt");
								try {
									st.createTag(p);
								} catch (MalformedURLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}	
							} catch (ClassNotFoundException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							//} catch (MalformedURLException e) {
								// TODO Auto-generated catch block
							//	e.printStackTrace();
							} catch (InstantiationException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (IllegalAccessException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						}

					}

				};
				applyButton.addListener(SWT.Selection, listener);
			}

			browseButton = new Button(PlugInGroup, SWT.PUSH | SWT.CENTER);
			FormData browseButtonLData = new FormData();
			browseButtonLData.left =  new FormAttachment(0, 1000, 396);
			browseButtonLData.top =  new FormAttachment(0, 1000, 60);
			browseButtonLData.width = 46;
			browseButtonLData.height = 24;
			browseButton.setLayoutData(browseButtonLData);
			browseButton.setText("Browse");
			browseButton.setEnabled(false);
			listener = new Listener()
			{

				@Override
				public void handleEvent(Event event) 
				{
					if(event.widget == browseButton)
					{
						//applyButton.setEnabled(true);
						//OPEN FILES DIALOG
						FileDialog fd = new FileDialog(getShell(), SWT.MULTI);
						fd.setText("Save");
						fd.setFilterPath("C:/");
						String[] filterExt = { "*.class" };
						fd.setFilterExtensions(filterExt);
						String selected = fd.open();
						System.out.println(selected);

						otherText.setText(selected);
						if(selected == ""){
							applyButton.setEnabled(false);
						}
						else{
							applyButton.setEnabled(true);
						}

					}

				}

			};
			//	update();
			browseButton.addListener(SWT.Selection, listener);


			FormData otherTextLData = new FormData();
			otherTextLData.left =  new FormAttachment(0, 1000, 99);
			otherTextLData.top =  new FormAttachment(0, 1000, 61);
			otherTextLData.width = 273;
			otherTextLData.height = 16;
			otherText = new Text(PlugInGroup, SWT.MULTI | SWT.BORDER);
			otherText.setLayoutData(otherTextLData);
			otherText.setEnabled(false);
			//otherText.setEditable(false);
			listener = new Listener() {

				@Override
				public void handleEvent(Event arg0) {
					System.out.println(otherText.getText());
					if (otherText.getText() != ""){
						applyButton.setEnabled(true);
					}

					System.out.println(otherText.getText());

				}
			};
			otherText.addListener(SWT.Modify, listener);


			otherButton = new Button(PlugInGroup, SWT.RADIO | SWT.LEFT);
			FormData otherButtonLData = new FormData();
			otherButtonLData.left =  new FormAttachment(0, 1000, 32);
			otherButtonLData.top =  new FormAttachment(0, 1000, 61);
			otherButtonLData.width = 63;
			otherButtonLData.height = 16;
			otherButton.setLayoutData(otherButtonLData);
			otherButton.setText("Other:");
			listener = new Listener()
			{
				@Override
				public void handleEvent(Event event) 
				{
					if(event.widget == otherButton)
					{
						applyButton.setEnabled(false);
						//	otherText.setEnabled(true);
						if (otherText.getText() == "")
							browseButton.setEnabled(true);
						st.setCurr("other");
					}

				}

			};
			otherButton.addListener(SWT.Selection, listener);

			tag1Button = new Button(PlugInGroup, SWT.RADIO | SWT.LEFT);
			FormData tag1ButtonLData = new FormData();
			tag1ButtonLData.width = 92;
			tag1ButtonLData.height = 55;
			tag1ButtonLData.left =  new FormAttachment(64, 1000, 0);
			tag1ButtonLData.right =  new FormAttachment(248, 1000, 0);
			tag1ButtonLData.top =  new FormAttachment(5, 1000, 0);
			tag1ButtonLData.bottom =  new FormAttachment(578, 1000, 0);
			tag1Button.setLayoutData(tag1ButtonLData);
			tag1Button.setText("Tag by size");
			//	tag1Button.addSelectionListener(new Tag1Listener());
			listener = new Listener()
			{

				@Override
				public void handleEvent(Event event) 
				{
					if(event.widget == tag1Button)
					{
						applyButton.setEnabled(true);
						otherText.setText("");
						//	otherText.setEnabled(false);
						browseButton.setEnabled(false);
						st.setCurr("TagBySize");
					}

				}

			};
			//	update();
			tag1Button.addListener(SWT.Selection, listener);


			tag3Button = new Button(PlugInGroup, SWT.RADIO | SWT.LEFT);
			FormData tag3ButtonLData = new FormData();
			tag3ButtonLData.width = 109;
			tag3ButtonLData.height = 63;
			tag3ButtonLData.left =  new FormAttachment(765, 1000, 0);
			tag3ButtonLData.right =  new FormAttachment(983, 1000, 0);
			tag3ButtonLData.top =  new FormAttachment(-5, 1000, 0);
			tag3ButtonLData.bottom =  new FormAttachment(651, 1000, 0);
			tag3Button.setLayoutData(tag3ButtonLData);
			tag3Button.setText("Tag by suffix");
			//	tag3Button.addSelectionListener(new Tag3Listener());
			listener = new Listener()
			{

				@Override
				public void handleEvent(Event event) 
				{
					if(event.widget == tag3Button)
					{
						applyButton.setEnabled(true);
						otherText.setText("");
						//	otherText.setEnabled(false);
						browseButton.setEnabled(false);
						st.setCurr("TagBySuffix");
					}

				}

			};
			//	update();
			tag3Button.addListener(SWT.Selection, listener);


			tag2Button = new Button(PlugInGroup, SWT.RADIO | SWT.LEFT);
			FormData tag2ButtonLData = new FormData();
			tag2ButtonLData.width = 123;
			tag2ButtonLData.height = 50;
			tag2ButtonLData.left =  new FormAttachment(387, 1000, 0);
			tag2ButtonLData.right =  new FormAttachment(632, 1000, 0);
			tag2ButtonLData.top =  new FormAttachment(57, 1000, 0);
			tag2ButtonLData.bottom =  new FormAttachment(578, 1000, 0);
			tag2Button.setLayoutData(tag2ButtonLData);
			tag2Button.setText("Tag by key words");
			//	tag2Button.addSelectionListener(new Tag2Listener());
			listener = new Listener()
			{

				@Override
				public void handleEvent(Event event) 
				{
					if(event.widget == tag2Button)
					{
						applyButton.setEnabled(true);
						otherText.setText("");
						//	otherText.setEnabled(false);
						browseButton.setEnabled(false);
						st.setCurr("TagByKeyWord");

					}

				}

			};
			//	update();
			tag2Button.addListener(SWT.Selection, listener);

			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public String[] parseString(String str){

		String[] tokens = str.split(",");


		return tokens;

	}

	@Override
	public void update(){
		//boolean flag = otherButton.isEnabled();
		//	System.out.println(flag);
		//		otherButton.setEnabled(flag);
		//	browseButton.setEnabled(flag);
	}
}



